<?php
include_once('open-flash-chart.php');

class Helper_Graphic{
	
	private $Graphic;
	
	function __construct(){
		$this->Graphic = new graph();
		$this->Graphic->set_base("View/");
		$this->Graphic->set_js_path("js/");
		$this->Graphic->set_swf_path("Front/images/");
		//$this->Graphic->set_bg_image("View/Front/images/st.png","left","top");
		$this->Graphic->set_bg_colour("#FFFFFF");
	}
	
	function reset(){
		$this->__construct();		
	}
	
	function pie($pie_title,$pie_value,$pie_style=array()){
		
		$this->reset();
		
		arsort($pie_value);
		
		$this->generateBaseStyle($pie_style);
		
		$this->Graphic->set_title($pie_title,$pie_style['title_style']);
		$this->Graphic->set_width($pie_style['width']);
		$this->Graphic->set_height($pie_style['height']);
		
		$this->Graphic->pie( $alpha=60,$line_colour="#9933CC", $label_colour="#000000", $gradient = true, $border_size = false );
		$this->Graphic->pie_values( $pie_value, array_keys($pie_value) );
		$this->Graphic->pie_slice_colours( $this->getColors(count($pie_value)) );
		$this->Graphic->set_unique_id();
		return $this->Graphic->render("js");
	}
	
	//Line of Defects Qty & PPM  plus bar of Output
	function lineBar($title="",$width=500,$x_lables=array(),$ppm=array(),$output=array(),$ppm_range=array(),$output_range=array()){
		$this->reset();
		//Two line use the same X axis, the same Y axis(left)
		//The bar use the X axis as the line,but the Y axis is at the right
		
		#	title		1
		#	x_lable		1
		#	y_lable		line 1, bar 1
		#	x_legend	1
		#	y_legend	line 1, bar 1
		#	data		3
		#	y_min		2
		#	y_max		2	
		count($ppm_range) == 0 && $ppm_range = $this->computeDefaultRange($ppm,'top');
		count($output_range) == 0 && $output_range = $this->computeDefaultRange($output,'bottom');
		
		//default range
		
		$style = array();
		$style['width'] = $width;
		$this->generateBaseStyle($style);
		!empty($title) && $this->Graphic->set_title($title,$style['title_style']);
		$this->Graphic->set_width($style['width']);
		$this->Graphic->set_height($style['height']);
		
		$colors[] = "#CC3399";
		$colors[] = "#3334AD";
		
		
		$this->Graphic->line_hollow( $width=3, $dot_size=4, $colour=$colors[0], $line_name="Defects", $font_size=10);
		$this->Graphic->bar_glass( 60, $colors[1],$colors[1], "Output",10);
		$this->Graphic->set_x_labels($x_lables);
		$this->Graphic->set_y_min($ppm_range[0]);
		$this->Graphic->set_y_max($ppm_range[1]);
		
		$this->Graphic->set_y_right_min($output_range[0]);
		$this->Graphic->set_y_right_max($output_range[1]);
		$this->Graphic->y_axis_colour($colors[0]);
		
		$this->Graphic->x_axis_colour('#666666',"#CCCCCC");
		
		$this->Graphic->attach_to_y_right_axis(2);
		$this->Graphic->set_x_legend( "Date", $size=12, $colour='#FF0000' );
		$this->Graphic->set_y_legend( "Defects (PPM)", $size=12, $colors[0]);
		$this->Graphic->set_y_right_legend( "Output (K)", $size=12, $colors[1]);
		$this->Graphic->y_right_axis_colour( $colors[1]);
		
		$this->Graphic->set_tool_tip( "#x_legend#:#x_label#<br>#key#:#val#");
		$this->Graphic->set_data($ppm);
		$this->Graphic->set_data($output);
		
		$this->Graphic->set_unique_id();
		return $this->Graphic->render("js");
	}
	
	
	function generateBaseStyle(&$style){
		
		//set title style
		empty($style['title_style'])&&$style['title_style'] = "{font-size:20px;	font-family:Verdana; color:#666666}";
		
		//set size
		empty($style['width'])&&$style['width'] = 500;
		empty($style['height'])&&$style['height'] = 400;
		
	}
		
	function getColors($number){
		$colors = explode(',',"#FF0000,#3366CC,#FF00FF,#FF9999,#FF6600,#FFCC00,#66CC00,#00FF00,#FF00FF,#999900,#006600,#660000,#666666,#993333");
		$colors_cnt = count($colors);
		$ret = array();
		for ($i=0; $i<$number;$i++){
			$ret[] = $colors[$i%$colors_cnt];
		}
		return $ret;
	}
	function computeDefaultRange($data,$position = "center"){
		$max = max($data);
 		$min = min($data);
 		$difference = $max - $min;
 		
 		switch ($position){
 			case 'center' :
		 		$range_min = $min-$difference*0.2;
		 		$range_max = $max + $difference*0.2;
		 		break;
 			case 'top'	:
		 		$range_min = $min-$difference*1;
		 		$range_max = $max+$difference*0.1;
		 		break;
 			case 'bottom':
		 		$range_min = $min-$difference*0.1;
		 		$range_max = $max+$difference*1;
 				break;
 		}
		$range_min < 0 && $range_min = 0;
		
 		return array($range_min,$range_max);
	}

}


################## TEST ##################
/*
function testPie(){
	$img = new Helper_Graphic();
	echo "<body>";
	echo $img->pie("hello",array("John"=>10,"Harry"=>3,"Joey"=>2,"Mary"=>4,"Bob"=>7,"John2"=>5,"Harry2"=>3,"Joey2"=>2,"Mary2"=>4,"Bob2"=>7));
	echo "</body>";
}

function testLineBar(){

	$img = new Helper_Graphic();
	$x_lables = range(1,30,1);

	for ($i=0; $i<30; $i++){
		$ppm[$i] = rand(10,20);
	}

	for ($i=0;$i<30; $i++){
		$output[$i] = rand(30,50);
	}
	
	$ppm_range = array(1,20);
	$output_range = array(10,100);
	$width = 800;
	
	echo "<body>";
	echo $img->lineBar("30days Packing defects trend",$width,$x_lables,$ppm,$output);
	echo "</body>";
	
}

testLineBar();
*/